package com.qxzn.module.mine.activity;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.AppCompatEditText;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSONObject;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.bean.EmergencyContactBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.widget.ToastUtil;
import com.qxzn.app.util.CommonUtil;
import com.qxzn.app.util.SharePreferenceUtil;
import com.qxzn.module.mine.R;
import com.qxzn.module.mine.R2;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AddEmergencyContactActivity extends BaseActivity {
    EmergencyContactBean emergencyContactBean = null;

    @BindView(R2.id.name)
    AppCompatEditText name;
    @BindView(R2.id.phone)
    AppCompatEditText phone;

    @Override
    protected int initLayout() {
        return R.layout.activity_add_emergency_contact;
    }

    @Override
    protected void initView() {
        Bundle bean = getIntent().getBundleExtra("bean");
        if (bean != null) {
            tv_title.setText("修改紧急联系人");
            emergencyContactBean = (EmergencyContactBean) bean.getSerializable("EmergencyContactBean");
            name.setText(emergencyContactBean.name);
            name.setSelection(name.length());
            phone.setText(emergencyContactBean.phone);
            phone.setSelection(phone.length());
        } else {
            tv_title.setText("添加紧急联系人");
        }
        tv_right.setVisibility(View.VISIBLE);
        tv_right.setText("完成");
        tv_right.setOnClickListener(this);
        tv_right.setTextColor(Color.parseColor("#2A8CFF"));
        name.setOnTouchListener(this::onTouch);
        phone.setOnTouchListener(this::onTouch);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (CommonUtil.isFastClick()) return;
        if (view.getId() == R.id.tv_right) {
            String nameStr = name.getText().toString().trim();
            if (TextUtils.isEmpty(nameStr)) {
                ToastUtil.showTextToast(this, "请输入联系人名");
                return;
            }
            String phoneStr = phone.getText().toString().trim();
            if (!CommonUtil.isMobile(phoneStr)) {
                ToastUtil.showTextToast(this, "手机号格式有误");
                return;
            }

            showProgress();
            Callback<BaseBean> callback = new Callback<BaseBean>() {
                @Override
                public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                    hideProgress();
                    BaseBean bean = response.body();
                    if (bean.getCode() == 200) {
                        if (emergencyContactBean != null)
                            ToastUtil.showTextToast(getApplicationContext(), "修改成功");
                        else
                            ToastUtil.showTextToast(getApplicationContext(), "添加成功");
                        setResult(RESULT_OK);
                        finish();
                    } else {
                        ToastUtil.showTextToast(AddEmergencyContactActivity.this, bean.getMsg());
                    }
                }

                @Override
                public void onFailure(Call<BaseBean> call, Throwable t) {
                    hideProgress();
                    ToastUtil.showTextToast(AddEmergencyContactActivity.this, getString(R.string.network_error));
                }
            };

            JSONObject params = new JSONObject();
            params.put("name", nameStr);
            params.put("phone", phoneStr);
            params.put("device_id", SharePreferenceUtil.getDeviceId(this));
            if (emergencyContactBean != null) {
                params.put("id", emergencyContactBean.emergency_id);
                HttpRequest.getService().updateEmergencyContact(HttpRequest.getDefaultBody(params)).enqueue(callback);
            } else {
                HttpRequest.getService().addEmergencyContact(HttpRequest.getDefaultBody(params)).enqueue(callback);
            }
        }
    }

    @Override
    protected void iniTitleBar() {
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    public boolean onTouch(View v, MotionEvent event) {
        EditText editText = (EditText) v;
        // et.getCompoundDrawables()得到一个长度为4的数组，分别表示左右上下四张图片
        Drawable drawable = editText.getCompoundDrawables()[2];
        //如果右边没有图片，不再处理
        if (drawable == null)
            return false;
        //如果不是按下事件，不再处理
        if (event.getAction() != MotionEvent.ACTION_UP)
            return false;
        if (event.getX() > editText.getWidth()
                - editText.getPaddingRight()
                - drawable.getIntrinsicWidth()) {
            editText.setText("");
        }
        return false;
    }
}
